﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Transactions;
using DbMapping;
using DbMapping.Common;
using VideoCache.Server.DAL;
using VideoCache.Server.Model;
using VideoCache.Spider;

namespace VideoCache.Server.BLL
{
    public partial class AlbumBLL
    {
        public static int CreateAlbum(AlbumInfo album, AlbumDescriptionInfo description)
        {
            using (IDbConnection conn = DbAccess.GetConnection())
            {
                conn.Open();
                using (IDbTransaction tran = conn.BeginTransaction())
                {
                    try
                    {
                        album.CreateTime = DateTime.Now;
                        album.ModifyTime = DateTime.Now;

                        album.Id = AlbumDAL.Add(tran, album);

                        description.AlbumId = album.Id;

                        AlbumDescriptionDAL.Add(tran, description);

                        tran.Commit();

                        return album.Id;
                    }
                    catch (Exception ex)
                    {
                        tran.Rollback();
                        throw new Exception("保存视频失败。", ex);
                    }
                }
            }
        }

        public static void EditAlbum(AlbumInfo album, AlbumDescriptionInfo description)
        {
            AlbumInfo info = AlbumBLL.Find(album.Id);

            if (info == null) return;

            using (IDbConnection conn = DbAccess.GetConnection())
            {
                conn.Open();
                using (IDbTransaction tran = conn.BeginTransaction())
                {
                    try
                    {
                        album.ModifyTime = DateTime.Now;

                        AlbumDAL.Update(tran, info);

                        description.AlbumId = album.Id;

                        AlbumDescriptionDAL.Update(tran, description);

                        tran.Commit();
                    }
                    catch (Exception ex)
                    {
                        tran.Rollback();
                        throw new Exception("保存视频失败。", ex);
                    }
                }
            }
        }

        public static void DeleteAlbum(int id)
        {
            using (IDbConnection conn = DbAccess.GetConnection())
            {
                conn.Open();
                using (IDbTransaction tran = conn.BeginTransaction())
                {
                    try
                    {
                        AlbumDAL.Delete(tran, id);
                        AlbumDescriptionDAL.Delete(tran, id);

                        Conditions conditions = new Conditions();
                        conditions.Add(new ConditionItem(LinkType.And, EpisodeInfo.AlbumIdField, CompareType.Equal, id));
                        EpisodeDAL.Delete(tran, conditions);

                        tran.Commit();
                    }
                    catch (Exception ex)
                    {
                        tran.Rollback();
                        throw new Exception("保存视频失败。", ex);
                    }
                }
            }
        }

        public static void Delete(int[] list)
        {
            using (IDbConnection conn = DbAccess.GetConnection())
            {
                conn.Open();
                using (IDbTransaction tran = conn.BeginTransaction())
                {
                    try
                    {
                        foreach (int id in list)
                        {
                            AlbumDAL.Delete(tran, id);
                            AlbumDescriptionDAL.Delete(tran, id);

                            Conditions cons = new Conditions();
                            cons.Add(new ConditionItem(LinkType.And, EpisodeInfo.AlbumIdField, CompareType.Equal, id));

                            EpisodeDAL.Delete(tran, cons);
                        }
                    }
                    catch (Exception ex)
                    {
                        tran.Rollback();
                        throw new Exception("删除视频列表失败。", ex);
                    }
                }
            }
        }

        public static AlbumInfo[] SearchFromBaidu(string keyword)
        {
            var list = SpiderFactory.SearchAlbum(keyword);

            List<AlbumInfo> albumList = new List<AlbumInfo>();

            foreach (var item in list)
            {
                AlbumInfo info = new AlbumInfo();
                info.Title = item.title;
                info.PubYear = Convert.ToInt32(item.al_date);
                info.BaiduId = Convert.ToInt32(item.id);

                if (item.director != null)
                {
                    foreach (string director in item.director)
                    {
                        if (!string.IsNullOrEmpty(info.Director))
                        {
                            info.Director += ",";
                        }
                        info.Director += director;
                    }
                }

                if (item.actor != null)
                {
                    foreach (string actor in item.actor)
                    {
                        if (!string.IsNullOrEmpty(info.Actor))
                        {
                            info.Actor += ",";
                        }
                        info.Actor += actor;
                    }
                }

                albumList.Add(info);
            }

            return albumList.ToArray();
        }
    }
}